package com.example.shiwu.util;

import com.google.common.primitives.Bytes;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ArrayUtil {
    public static String addArrays(String[]... arrays) {
        // 确定数组长度
        int length = arrays[0].length;
        // 创建新数组
        String[] result = new String[length];
        String addStr = "";
        // 对应位置相加
        for (int i = 0; i < length; i++) {
            for (String[] array : arrays) {
                //result[i] += array[i];
                addStr+=doubleAdd(result[i],array[i])+",";
            }
        }

        // 返回结果
        return addStr;
    }

    /**
     * Stirng 数组 转 double 数组相加
     *
     * @param d1
     * @param d2
     * @return
     */
    public static Double doubleAdd(String d1,String d2){
        //这里一定要将double数据转化为String后再转化bigDecimal 不然数据计算还是会失去精度
        if(d1 == null){
            d1 = "0";
        }
        BigDecimal decimal1 = new BigDecimal(d1);
        BigDecimal decimal2 = new BigDecimal(d2);
        return decimal1.add(decimal2).doubleValue();
    }

    public static BigDecimal stringToDouble(String d1){

        //System.out.println("-------------stringToDouble1-------------------  "+d1);
        String d2 = d1.trim();
        //这里一定要将double数据转化为String后再转化bigDecimal 不然数据计算还是会失去精度
        //System.out.println("-------------stringToDouble2-------------------  "+d2);
        BigDecimal decimal1 = new BigDecimal(d2);
        return decimal1;
    }

    public static void main(String[] args) {
        stringToDouble("1.9740319848060608");
    }

    /**
     * 1,string 转 BigDecimal
     * 2，BigDecimal 相加
     * 3.求平均数
     * @param strLis
     * @return
     */
    public static String strToStrArrrayAdd(String[] strLis){
        ArrayList<String[]> ay = new ArrayList<String[]>();
        for (int i = 0; i < strLis.length; i++) {
            String[] strLisChild = strLis[i].split(",");
            ay.add(strLisChild);
        }
        // 确定数组长度
        int length = ay.get(0).length;
        // 确定数组长度
        int aylength = ay.size();
        System.out.println("------------ay.size()-----------"+aylength);
        // 创建新数组
        String[] result = new String[length];
        BigDecimal[] result1 = new BigDecimal[length];

        String addStr = "";
        StringBuilder resultBuilder = new StringBuilder();
        try {
            // 对应位置相加
            for (int i = 0; i < length; i++) {
                for (String[] array : ay) {
                    if(!array[i].equals(" ")){
                        BigDecimal bd = stringToDouble(array[i].trim());
                        if(result1[i] == null){
                            result1[i] = BigDecimal.valueOf(0);
                        }
                        result1[i] = result1[i].add(bd);//四则运算-加法
                    }


                }
            }

            // 取平均数 拼接string
            for (int i = 0; i < result1.length; i++) {
                //System.out.println("result1[i] ===== "+result1[i]+"   "+aylength);
                if(result1[i] == null){
                    break;
                }
                BigDecimal average_bd = result1[i].divide(BigDecimal.valueOf(aylength),5, BigDecimal.ROUND_HALF_UP);
                //System.out.println("average_bd ===== "+average_bd);
                resultBuilder.append(average_bd);
            }
            addStr = resultBuilder.toString();
        }catch (Exception e){
            e.printStackTrace();
        }


        return addStr;
    }

    /**
     * 1,string 转 BigDecimal
     * 2，BigDecimal 相加
     * 3.求平均数
     * @param strLis
     * @return
     */
    public static String strToStrArrrayAddDouble(String[] strLis){
        ArrayList<String[]> ay = new ArrayList<String[]>();
        for (int i = 0; i < strLis.length; i++) {
            String[] strLisChild = strLis[i].split(",");
            ay.add(strLisChild);
        }
        // 确定数组长度
        int length = ay.get(0).length;
        // 确定数组长度
        int aylength = ay.size();
        System.out.println("------------ay.size()-----------"+aylength);
        // 创建新数组
        String[] result = new String[length];
        Double[] result1 = new Double[length];

        String addStr = "";
        StringBuilder resultBuilder = new StringBuilder();
        try {
            // 对应位置相加
            for (int i = 0; i < length; i++) {
                for (String[] array : ay) {
                    if(!array[i].equals(" ")){
                        Double bd = Double.valueOf(array[i].trim());
                        if(result1[i] == null){
                            result1[i] = Double.valueOf(0);
                        }
                        result1[i] = result1[i]+=(bd);//四则运算-加法
                    }


                }
            }

            // 取平均数 拼接string
            for (int i = 0; i < result1.length; i++) {
                //System.out.println("result1[i] ===== "+result1[i]+"   "+aylength);
                if(result1[i] == null){
                    break;
                }
                Double average_bd = result1[i]/aylength;
                //System.out.println("average_bd ===== "+average_bd);
                resultBuilder.append(average_bd+",");
            }
            addStr = resultBuilder.toString();
        }catch (Exception e){
            e.printStackTrace();
        }


        return addStr;
    }

    /**
     * 截取byte数组   不改变原数组
     * @param b 原数组
     * @param off 偏差值（索引）
     * @param length 长度
     * @return 截取后的数组
     */
    public static byte[] subByte(byte[] b, int off, int length){
        byte[] b1 = new byte[length];
        System.arraycopy(b, off, b1, 0, length);
        return b1;
    }

    /**
     * 合并byte[]数组 （不改变原数组）
     * @param byte_1
     * @param byte_2
     * @return 合并后的数组
     */
    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){
        byte[] byte_3 = new byte[byte_1.length+byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }

    /**
     * List<Byte> 转 byte[]
     * @param list
     * @return
     */
    public static byte[] listTobyte(List<Byte> list) {

        byte[] bytes= Bytes.toArray(list);

        return bytes;
    }
}
